<template>
   
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              style="width: 203px"
              v-model="listQuery.keyword"
              placeholder="楼盘名称"
            ></el-input>
          </el-form-item>
    
          <el-form-item label="楼盘等级：">
            <el-select
              v-model="listQuery.brandId"
              placeholder="请选择等级"
              clearable
            >
              <el-option
                v-for="item in brandOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select
              v-model="listQuery.publishStatus"
              placeholder="全部"
              clearable
            >
              <el-option
                v-for="item in publishStatusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
     
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button class="btn-add" @click="handleAddProduct()" size="mini">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="编号" width="50" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="楼盘图片" width="120" align="center">
          <template slot-scope="scope">
            <!-- <img style="height: 80px" src="../../../../static/img1.png"> -->
            <img style="height: 80px" :src="scope.row.url" />
            <!-- <span>{{scope.row.url}}</span> -->
          </template>
        </el-table-column>
        <el-table-column label="楼盘名称" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.name }}</p>
          </template>
        </el-table-column>
        <el-table-column label="广告售价（/梯/周）" width="120" align="center">
          <template slot-scope="scope">
            <p>15s ￥{{ scope.row.price }}</p>
            <p>10s ￥{{ scope.row.price10 }}</p>
            <p>5s ￥{{ scope.row.price5 }}</p>
          </template>
        </el-table-column>
        <el-table-column label="投放状态" width="140" align="center">
          <template slot-scope="scope">
            <p>
              {{ scope.row.status | loupanStatusFilter }}
              <el-switch
                @change="handlePublishStatusChange(scope.$index, scope.row)"
                :active-value="1"
                :inactive-value="0"
                v-model="scope.row.status"
              >
              </el-switch>
            </p>
          </template>
        </el-table-column>
        <el-table-column label="地址" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.address }}</template>
        </el-table-column>
        <el-table-column label="经度" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.longitude }}</template>
        </el-table-column>
        <el-table-column label="纬度" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.latitude }}</template>
        </el-table-column>
        <el-table-column label="电梯号" width="100" align="center">
          <template slot-scope="scope">
            <div v-for="item in scope.row.liftArr" :key="item.name">
              <p>{{ item.name }}</p>
              <!-- 考虑是否展示 -->
              <!-- <p>投放状态：{{ item.check ? "已投放" : "未投放" }}</p> -->
              <p>图片：</p>
              <img :src="item.url" alt="" v-if="item.url"/>
              <p v-else>未添加</p>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="SKU库存" width="100" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleShowSkuEditDialog(scope.$index, scope.row)" circle></el-button>
          </template>
        </el-table-column> -->
        <el-table-column label="楼盘等级" width="100" align="center">
          <template slot-scope="scope">{{
            loupansize(scope.row.size)
          }}</template>
        </el-table-column>
        <el-table-column label="入住规模" width="100" align="center">
          <template slot-scope="scope"> {{ scope.row.Cheekin }} </template>
        </el-table-column>
        <el-table-column label="入住率（%）" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.CheekinPercent }}</template>
        </el-table-column>
        <el-table-column label="最高楼层数" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.tall }}</template>
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <p>
              <el-button
                size="mini"
                @click="handleShowProduct(scope.$index, scope.row)"
                >查看
              </el-button>
              <el-button
                size="mini"
                @click="handleUpdateProduct(scope.$index, scope.row)"
                >编辑
              </el-button>
            </p>
            <p>
              <el-button
                size="mini"
                @click="handleShowLog(scope.$index, scope.row)"
                >日志
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除
              </el-button>
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small"
      >
        批量删除
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
 
  </div>
</template>
<script>
import {
  fetchList,
  updateDeleteStatus,
  updateNewStatus,
  updateRecommendStatus,
  updatePublishStatus,
} from "@/api/product";
import {
  fetchList as fetchSkuStockList,
  update as updateSkuStockList,
} from "@/api/skuStock";
import { fetchList as fetchProductAttrList } from "@/api/productAttr";
import { fetchList as fetchBrandList } from "@/api/brand";
import { fetchListWithChildren } from "@/api/productCate";
import {
  getAllLlift,
  changeliftstatus,
  deleteloupanArr,
  selectLoupan,
} from "@/api/aself";
const defaultListQuery = {
  keyword: null,
  pageNum: 1,
  pageSize: 5,
  publishStatus: null,
  verifyStatus: null,
  productSn: null,
  productCategoryId: null,
  brandId: null,
};
export default {
  name: "productList",
  data() {
    return {
      editSkuInfo: {
        dialogVisible: false,
        productId: null,
        productSn: "",
        productAttributeCategoryId: null,
        stockList: [],
        productAttr: [],
        keyword: null,
      },
      operates: [
        {
          label: "商品上架",
          value: "publishOn",
        },
        {
          label: "商品下架",
          value: "publishOff",
        },
        {
          label: "设为推荐",
          value: "recommendOn",
        },
        {
          label: "取消推荐",
          value: "recommendOff",
        },
        {
          label: "设为新品",
          value: "newOn",
        },
        {
          label: "取消新品",
          value: "newOff",
        },
        {
          label: "转移到分类",
          value: "transferCategory",
        },
        {
          label: "移入回收站",
          value: "recycle",
        },
      ],
      operateType: null,
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: true,
      selectProductCateValue: null,
      multipleSelection: [],
      productCateOptions: [],
      brandOptions: [
        { label: "TOP", value: 1 },
        { label: "5A", value: 2 },
        { label: "4A", value: 3 },
        { label: "3A", value: 4 },
        { label: "T-ap", value: 5 },
        { label: "H-ap", value: 6 },
      ], //考虑是否新建等级表接口获取 目前写死
      publishStatusOptions: [
        {
          value: 1,
          label: "可投放",
        },
        {
          value: 0,
          label: "不可投放",
        },
      ],
      verifyStatusOptions: [
        {
          value: 1,
          label: "审核通过",
        },
        {
          value: 0,
          label: "未审核",
        },
      ],
    };
  },
  created() {
    this.getList();
    // 转状态为number

    // this.getBrandList();
    // this.getProductCateList();
  },
  mounted() {},
  watch: {
    selectProductCateValue: function (newValue) {
      if (newValue != null && newValue.length == 2) {
        this.listQuery.productCategoryId = newValue[1];
      } else {
        this.listQuery.productCategoryId = null;
      }
    },
  },
  filters: {
    verifyStatusFilter(value) {
      if (value === 1) {
        return "审核通过";
      } else {
        return "未审核";
      }
    },
    loupanStatusFilter(value) {
      if (value === 1) {
        return "可投放";
      } else {
        return "暂不可投放";
      }
    },
  },
  methods: {
    // 转为数字类型
    getNumberStatus(status) {
      return Number(status);
    },
    // 展示楼盘类型
    loupansize(size) {
      // size为数字类型
      let sizeType = "";
      switch (size) {
        case 1:
          sizeType = "TOP";
          break;
        case 2:
          sizeType = "5A";
          break;
        case 3:
          sizeType = "4A";
          break;
        case 4:
          sizeType = "3A";
          break;
        case 5:
          sizeType = "T-ap";
          break;
        case 6:
          sizeType = "H-ap";
          break;
        default:
          sizeType = "unKnown";
          break;
      }
      // console.log('type==',sizeType,size);
      return sizeType;
    },
    getProductSkuSp(row, index) {
      let spData = JSON.parse(row.spData);
      if (spData != null && index < spData.length) {
        return spData[index].value;
      } else {
        return null;
      }
    },
    getList() {
      // 获取楼盘信息
      this.listLoading = true;
      const { pageNum, pageSize } = this.listQuery;
      getAllLlift({ page: pageNum, pageSize }).then((response) => {
        this.listLoading = false;
        // console.log('获取楼盘信息--',response.data);
        this.list = response.data.pagelist;
        this.list.forEach((row) => {
          row.status = Number(row.status);
        });
        this.total = response.data.total;
      });
      // fetchList(this.listQuery).then(response => {
      //   this.listLoading = false;
      //   this.list = response.data.list;
      //   this.total = response.data.list.length;
      // });
    },
    getBrandList() {
      fetchBrandList({ pageNum: 1, pageSize: 100 }).then((response) => {
        this.brandOptions = [];
        let brandList = response.data.list;
        for (let i = 0; i < brandList.length; i++) {
          this.brandOptions.push({
            label: brandList[i].name,
            value: brandList[i].id,
          });
        }
      });
    },
    getProductCateList() {
      fetchListWithChildren().then((response) => {
        let list = response.data;
        this.productCateOptions = [];
        for (let i = 0; i < list.length; i++) {
          let children = [];
          if (list[i].children != null && list[i].children.length > 0) {
            for (let j = 0; j < list[i].children.length; j++) {
              children.push({
                label: list[i].children[j].name,
                value: list[i].children[j].id,
              });
            }
          }
          this.productCateOptions.push({
            label: list[i].name,
            value: list[i].id,
            children: children,
          });
        }
      });
    },
    handleShowSkuEditDialog(index, row) {
      this.editSkuInfo.dialogVisible = true;
      this.editSkuInfo.productId = row.id;
      this.editSkuInfo.productSn = row.productSn;
      this.editSkuInfo.productAttributeCategoryId =
        row.productAttributeCategoryId;
      this.editSkuInfo.keyword = null;
      fetchSkuStockList(row.id, { keyword: this.editSkuInfo.keyword }).then(
        (response) => {
          this.editSkuInfo.stockList = response.data;
        }
      );
      if (row.productAttributeCategoryId != null) {
        fetchProductAttrList(row.productAttributeCategoryId, { type: 0 }).then(
          (response) => {
            this.editSkuInfo.productAttr = response.data.list;
          }
        );
      }
    },
    handleSearchEditSku() {
      fetchSkuStockList(this.editSkuInfo.productId, {
        keyword: this.editSkuInfo.keyword,
      }).then((response) => {
        this.editSkuInfo.stockList = response.data;
      });
    },
    handleEditSkuConfirm() {
      if (
        this.editSkuInfo.stockList == null ||
        this.editSkuInfo.stockList.length <= 0
      ) {
        this.$message({
          message: "暂无sku信息",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      this.$confirm("是否要进行修改", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        updateSkuStockList(
          this.editSkuInfo.productId,
          this.editSkuInfo.stockList
        ).then((response) => {
          this.$message({
            message: "修改成功",
            type: "success",
            duration: 1000,
          });
          this.editSkuInfo.dialogVisible = false;
        });
      });
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      const { brandId, keyword, publishStatus, pageNum, pageSize } =
        this.listQuery;
      const obj = {
        type: brandId,
        name: keyword,
        status: publishStatus,
        page: pageNum,
        pageSize,
      };
      // console.log(obj);
      selectLoupan(obj).then((response) => {
        console.log(response.data);
        this.list = response.data.pagelist;
        this.total = response.data.total;
        // 将status改为number
        this.list.forEach((row) => {
          row.status = Number(row.status);
        });
      });
      // console.log(this.listQuery);
      // this.getList();
    },
    handleAddProduct() {
      // 跳转到添加楼盘界面
      this.$router.push({ path: "/pms/addProduct" });
    },
    // 批量删除
    handleBatchOperate() {
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message({
          message: "请选择要删除的商品",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      this.$confirm("是否要进行该批量删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        console.log("要删除的楼盘", ids);
        // 调用接口遍历删除楼盘
        deleteloupanArr(ids).then((response) => {
          if (response.isSuccee) {
            this.$message({
              message: "删除成功",
              type: "success",
              duration: 1000,
            });
            // this.$router.push('/pms/product')
            this.getList();
          }
        });
      });
    },
    handleSizeChange(val) {
      // 修改每页数据条数
      this.listQuery.pageNum = 1; //页码
      this.listQuery.pageSize = val; //每页数据
      this.getList();
    },
    handleCurrentChange(val) {
      //修改页码
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // console.log(val);
      // 获取当前被选中的楼盘列表
    },
    handlePublishStatusChange(index, row) {
      // 修改楼盘投放状态
      // console.log(row.id, index);
      changeliftstatus({ id: row.id }).then((response) => {
        if (response.isSuccee) {
          this.$message({
            message: "修改投放状态成功",
            type: "success",
            duration: 1000,
          });
        }
      });
      // this.updatePublishStatus(row.id, index);
    },
    handleNewStatusChange(index, row) {
      let ids = [];
      ids.push(row.id);
      this.updateNewStatus(row.newStatus, ids);
    },
    handleRecommendStatusChange(index, row) {
      let ids = [];
      ids.push(row.id);
      this.updateRecommendStatus(row.recommandStatus, ids);
    },
    handleResetSearch() {
      this.selectProductCateValue = [];
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleDelete(index, row) {
      this.$confirm("是否要进行删除操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = [];
        ids.push(row.id);
        this.updateDeleteStatus(1, ids);
      });
    },
    handleUpdateProduct(index, row) {
      this.$router.push({ path: "/pms/updateProduct", query: { id: row.id } });
    },
    handleShowProduct(index, row) {
      console.log("handleShowProduct", row);
    },
    handleShowVerifyDetail(index, row) {
      console.log("handleShowVerifyDetail", row);
    },
    handleShowLog(index, row) {
      console.log("handleShowLog", row);
    },
    updatePublishStatus(publishStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("publishStatus", publishStatus);
      updatePublishStatus(params).then((response) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    updateNewStatus(newStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("newStatus", newStatus);
      updateNewStatus(params).then((response) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    updateRecommendStatus(recommendStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("recommendStatus", recommendStatus);
      updateRecommendStatus(params).then((response) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    updateDeleteStatus(deleteStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("deleteStatus", deleteStatus);
      updateDeleteStatus(params).then((response) => {
        this.$message({
          message: "删除成功",
          type: "success",
          duration: 1000,
        });
      });
      this.getList();
    },
  },
};
</script>
<style scoped>
.el-table .cell img{
 
    width: 100px;
    height: auto !important;
  
}
</style>>


